<?php

namespace app\admin\service;

use app\admin\model\UserList;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class UserService
{
    public static function goAdd()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'account|账号' => 'require|min:5',
                'password|密码' => 'require|min:6|max:20',
                'name|名称' => 'require',
                'avatar|头像' => 'require',
                'level|等级' => 'require|min:1',
            ];
            $message = [
                'account.require' => "用户账号不能为空",
                'password.require' => "用户密码不能为空",
                'name.require' => "用户名称不能为空",
                'avatar.require' => "用户头像不能为空",
                'level.require' => "用户等级不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            $data['sign'] = uniqid();
            $data['password'] = authCode($data['password'], 'ENCODE');
            try {
                $exist = UserList::where('account', $data['account'])->find();
                if ($exist) return ['msg' => '账号已存在', 'code' => 201];
                $res = UserList::create($data);
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'account|账号' => 'require|min:5',
                'name|名称' => 'require',
                'avatar|头像' => 'require',
                'level|等级' => 'require|min:1',
            ];
            $message = [
                'account.require' => "用户账号不能为空",
                'name.require' => "用户名称不能为空",
                'avatar.require' => "用户头像不能为空",
                'level.require' => "用户等级不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            if ($data['password']) $data['password'] = authCode($data['password'], 'ENCODE');
            else unset($data['password']);
            try {
                $exist = UserList::where('account', $data['account'])->where('id', '<>', $data['id'])->find();
                if ($exist) return ['msg' => '账号已存在', 'code' => 201];
                $res = UserList::update($data);
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatus()
    {
        $data = Request::post();
        $model = UserList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemove()
    {
        $data = Request::post();
        $info = UserList::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}